﻿using Microsoft.Graph.DeviceManagement.UserExperienceAnalyticsBaselines.Item.WorkFromAnywhereMetrics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zhaoxi.Industrial.View
{
    /// <summary>
    /// SystemMonitor.xaml 的交互逻辑
    /// </summary>
    public partial class SystemMonitor : UserControl
    {
        public SystemMonitor()
        {
            InitializeComponent();
        }

        bool isMoving = false;
        double nLeft = 0;
        double nTop = 0;
        Point point = new Point(0,0);

        /// <summary>
        /// 鼠标滚轮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            //根据鼠标滚轮的数值增加图形长度
            double nWidth = this.vbox001.ActualWidth + e.Delta;
            double nHeight = this.vbox001.ActualHeight + e.Delta;

            Point mousePosition = e.GetPosition(this);

            //限制最小值
            if (nWidth < 100) nWidth = 100;
            if (nHeight < 100) nHeight = 100;

            this.vbox001.Width = nWidth;    
            this.vbox001.Height = nHeight;


            //整个屏幕的宽度 - canvas 的宽度/2  设置为左边距属性
            this.vbox001.SetValue(Canvas.LeftProperty, (this.RenderSize.Width - this.vbox001.Width)/2);

        }

        /// <summary>
        /// 鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isMoving = true;
            point = e.GetPosition(sender as Canvas);

            //拿到当前图形的上边距和左边距
            nTop = double.Parse(this.vbox001.GetValue(Canvas.TopProperty).ToString());//注意一定要在对象上先初始化 左边距和上边距 不然就是NaN
            nLeft = double.Parse(this.vbox001.GetValue(Canvas.LeftProperty).ToString());

            (sender as Canvas).CaptureMouse();//锁定事件只影响绘画图层
            e.Handled = true;//解除冒泡的问题
        }

        /// <summary>
        /// 鼠标离开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isMoving = false;
            (sender as Canvas).ReleaseMouseCapture();
        }
        
        /// <summary>
        /// 鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMoving)
            {   //获取鼠标在canvas内部的位置
                Point _currentpoint = e.GetPosition((sender as Canvas));
                //左边距 + (当前鼠标X轴位置 - 之前鼠标X轴的位置)
                this.vbox001.SetValue(Canvas.LeftProperty,nLeft + (_currentpoint.X - point.X));
                this.vbox001.SetValue(Canvas.TopProperty,nTop + (_currentpoint.Y - point.Y));

                e.Handled = true;
            }
        }
    }
}
